3 namespace MediaWiki\Tests\Storage
;
5 use CommentStoreComment
;
8 use InvalidArgumentException
;
10 use MediaWiki\Linker\LinkTarget
;
11 use MediaWiki\MediaWikiServices
;
12 use MediaWiki\Storage\BlobStoreFactory
;
13 use MediaWiki\Storage\IncompleteRevisionException
;
14 use MediaWiki\Storage\MutableRevisionRecord
;
15 use MediaWiki\Storage\RevisionRecord
;
16 use MediaWiki\Storage\RevisionStore
;
17 use MediaWiki\Storage\SlotRecord
;
18 use MediaWiki\Storage\SqlBlobStore
;
19 use MediaWikiTestCase
;
24 use Wikimedia\Rdbms\Database
;
25 use Wikimedia\Rdbms\DatabaseSqlite
;
26 use Wikimedia\Rdbms\FakeResultWrapper
;
27 use Wikimedia\Rdbms\LoadBalancer
;
28 use Wikimedia\Rdbms\TransactionProfiler
;
35 class RevisionStoreDbTest
extends MediaWikiTestCase
{
38 * @return LoadBalancer
40 private function getLoadBalancerMock( array $server ) {
41 $lb = $this->getMockBuilder( LoadBalancer
::class )
42 ->setMethods( [ 'reallyOpenConnection' ] )
43 ->setConstructorArgs( [ [ 'servers' => [ $server ] ] ] )
46 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
47 function ( array $server, $dbNameOverride ) {
48 return $this->getDatabaseMock( $server );
58 private function getDatabaseMock( array $params ) {
59 $db = $this->getMockBuilder( DatabaseSqlite
::class )
60 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
61 ->setConstructorArgs( [ $params ] )
64 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
65 $db->method( 'isOpen' )->willReturn( true );
70 public function provideDomainCheck() {
71 yield
[ false, 'test', '' ];
72 yield
[ 'test', 'test', '' ];
74 yield
[ false, 'test', 'foo_' ];
75 yield
[ 'test-foo_', 'test', 'foo_' ];
77 yield
[ false, 'dash-test', '' ];
78 yield
[ 'dash-test', 'dash-test', '' ];
80 yield
[ false, 'underscore_test', 'foo_' ];
81 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
85 * @dataProvider provideDomainCheck
86 * @covers \MediaWiki\Storage\RevisionStore::checkDatabaseWikiId
88 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
91 'wgDBname' => $dbName,
92 'wgDBprefix' => $dbPrefix,
96 $loadBalancer = $this->getLoadBalancerMock(
99 'dbDirectory' => '*dummy*',
101 'password' => 'test',
109 'trxProfiler' => new TransactionProfiler(),
110 'connLogger' => new \Psr\Log\
NullLogger(),
111 'queryLogger' => new \Psr\Log\
NullLogger(),
112 'errorLogger' => new \Psr\Log\
NullLogger(),
115 'tablePrefix' => $dbPrefix,
118 $db = $loadBalancer->getConnection( DB_REPLICA
);
120 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
121 ->disableOriginalConstructor()
124 $store = new RevisionStore(
127 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
131 $count = $store->countRevisionsByPageId( $db, 0 );
133 // Dummy check to make PhpUnit happy. We are really only interested in
134 // countRevisionsByPageId not failing due to the DB domain check.
135 $this->assertSame( 0, $count );
138 private function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
139 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
140 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
141 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
142 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
145 private function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
146 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
147 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
148 $this->assertEquals( $r1->getComment(), $r2->getComment() );
149 $this->assertEquals( $r1->getPageAsLinkTarget(), $r2->getPageAsLinkTarget() );
150 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
151 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
152 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
153 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
154 $this->assertEquals( $r1->getSize(), $r2->getSize() );
155 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
156 $this->assertEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
157 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
158 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
159 foreach ( $r1->getSlotRoles() as $role ) {
160 $this->assertEquals( $r1->getSlot( $role ), $r2->getSlot( $role ) );
161 $this->assertEquals( $r1->getContent( $role ), $r2->getContent( $role ) );
164 RevisionRecord
::DELETED_TEXT
,
165 RevisionRecord
::DELETED_COMMENT
,
166 RevisionRecord
::DELETED_USER
,
167 RevisionRecord
::DELETED_RESTRICTED
,
169 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
174 * @param mixed[] $details
176 * @return RevisionRecord
178 private function getRevisionRecordFromDetailsArray( $title, $details = [] ) {
179 // Convert some values that can't be provided by dataProviders
180 $page = WikiPage
::factory( $title );
181 if ( isset( $details['user'] ) && $details['user'] === true ) {
182 $details['user'] = $this->getTestUser()->getUser();
184 if ( isset( $details['page'] ) && $details['page'] === true ) {
185 $details['page'] = $page->getId();
187 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
188 $details['parent'] = $page->getLatest();
191 // Create the RevisionRecord with any available data
192 $rev = new MutableRevisionRecord( $title );
193 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
194 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
195 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
196 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
197 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
198 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
199 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
200 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
201 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
202 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
203 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
208 private function getRandomCommentStoreComment() {
209 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
212 public function provideInsertRevisionOn_successes() {
213 yield
'Bare minimum revision insertion' => [
214 Title
::newFromText( 'UTPage' ),
216 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
218 'comment' => $this->getRandomCommentStoreComment(),
219 'timestamp' => '20171117010101',
223 yield
'Detailed revision insertion' => [
224 Title
::newFromText( 'UTPage' ),
226 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
229 'comment' => $this->getRandomCommentStoreComment(),
230 'timestamp' => '20171117010101',
233 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
239 * @dataProvider provideInsertRevisionOn_successes
240 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
242 public function testInsertRevisionOn_successes( Title
$title, array $revDetails = [] ) {
243 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
245 $store = MediaWikiServices
::getInstance()->getRevisionStore();
246 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
248 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
249 $this->assertRevisionRecordsEqual( $rev, $return );
253 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
255 public function testInsertRevisionOn_blobAddressExists() {
256 $title = Title
::newFromText( 'UTPage' );
258 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
260 'comment' => $this->getRandomCommentStoreComment(),
261 'timestamp' => '20171117010101',
265 $store = MediaWikiServices
::getInstance()->getRevisionStore();
267 // Insert the first revision
268 $revOne = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
269 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
270 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
271 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
273 // Insert a second revision inheriting the same blob address
274 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( 'main' ) );
275 $revTwo = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
276 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
277 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
278 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
280 // Assert that the same blob address has been used.
282 $firstReturn->getSlot( 'main' )->getAddress(),
283 $secondReturn->getSlot( 'main' )->getAddress()
285 // And that different revisions have been created.
286 $this->assertNotSame(
287 $firstReturn->getId(),
288 $secondReturn->getId()
292 public function provideInsertRevisionOn_failures() {
294 Title
::newFromText( 'UTPage' ),
296 'comment' => $this->getRandomCommentStoreComment(),
297 'timestamp' => '20171117010101',
300 new InvalidArgumentException( 'At least one slot needs to be defined!' )
302 yield
'slot that is not main slot' => [
303 Title
::newFromText( 'UTPage' ),
305 'slot' => SlotRecord
::newUnsaved( 'lalala', new WikitextContent( 'Chicken' ) ),
306 'comment' => $this->getRandomCommentStoreComment(),
307 'timestamp' => '20171117010101',
310 new InvalidArgumentException( 'Only the main slot is supported for now!' )
312 yield
'no timestamp' => [
313 Title
::newFromText( 'UTPage' ),
315 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
316 'comment' => $this->getRandomCommentStoreComment(),
319 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
321 yield
'no comment' => [
322 Title
::newFromText( 'UTPage' ),
324 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
325 'timestamp' => '20171117010101',
328 new IncompleteRevisionException( 'comment must not be NULL!' )
331 Title
::newFromText( 'UTPage' ),
333 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
334 'comment' => $this->getRandomCommentStoreComment(),
335 'timestamp' => '20171117010101',
337 new IncompleteRevisionException( 'user must not be NULL!' )
342 * @dataProvider provideInsertRevisionOn_failures
343 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
345 public function testInsertRevisionOn_failures(
347 array $revDetails = [],
348 Exception
$exception ) {
349 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
351 $store = MediaWikiServices
::getInstance()->getRevisionStore();
353 $this->setExpectedException(
354 get_class( $exception ),
355 $exception->getMessage(),
356 $exception->getCode()
358 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
361 public function provideNewNullRevision() {
363 Title
::newFromText( 'UTPage' ),
364 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
368 Title
::newFromText( 'UTPage' ),
369 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
375 * @dataProvider provideNewNullRevision
376 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
378 public function testNewNullRevision( Title
$title, $comment, $minor ) {
379 $store = MediaWikiServices
::getInstance()->getRevisionStore();
380 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
381 $record = $store->newNullRevision(
382 wfGetDB( DB_MASTER
),
389 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
390 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
391 $this->assertEquals( $comment, $record->getComment() );
392 $this->assertEquals( $minor, $record->isMinor() );
393 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
397 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
399 public function testNewNullRevision_nonExistingTitle() {
400 $store = MediaWikiServices
::getInstance()->getRevisionStore();
401 $record = $store->newNullRevision(
402 wfGetDB( DB_MASTER
),
403 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
404 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
406 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
408 $this->assertNull( $record );
412 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
414 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
415 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
416 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
417 /** @var Revision $rev */
418 $rev = $status->value
['revision'];
420 $store = MediaWikiServices
::getInstance()->getRevisionStore();
421 $revisionRecord = $store->getRevisionById( $rev->getId() );
422 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
424 $this->assertGreaterThan( 0, $result );
426 $page->getRevision()->getRecentChange()->getAttribute( 'rc_id' ),
432 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
434 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
435 // This assumes that sysops are auto patrolled
436 $sysop = $this->getTestSysop()->getUser();
437 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
438 $status = $page->doEditContent(
439 new WikitextContent( __METHOD__
),
445 /** @var Revision $rev */
446 $rev = $status->value
['revision'];
448 $store = MediaWikiServices
::getInstance()->getRevisionStore();
449 $revisionRecord = $store->getRevisionById( $rev->getId() );
450 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
452 $this->assertSame( 0, $result );
456 * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
458 public function testGetRecentChange() {
459 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
460 $content = new WikitextContent( __METHOD__
);
461 $status = $page->doEditContent( $content, __METHOD__
);
462 /** @var Revision $rev */
463 $rev = $status->value
['revision'];
465 $store = MediaWikiServices
::getInstance()->getRevisionStore();
466 $revRecord = $store->getRevisionById( $rev->getId() );
467 $recentChange = $store->getRecentChange( $revRecord );
469 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
470 $this->assertEquals( $rev->getRecentChange(), $recentChange );
474 * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
476 public function testGetRevisionById() {
477 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
478 $content = new WikitextContent( __METHOD__
);
479 $status = $page->doEditContent( $content, __METHOD__
);
480 /** @var Revision $rev */
481 $rev = $status->value
['revision'];
483 $store = MediaWikiServices
::getInstance()->getRevisionStore();
484 $revRecord = $store->getRevisionById( $rev->getId() );
486 $this->assertSame( $rev->getId(), $revRecord->getId() );
487 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
488 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
492 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
494 public function testGetRevisionByTitle() {
495 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
496 $content = new WikitextContent( __METHOD__
);
497 $status = $page->doEditContent( $content, __METHOD__
);
498 /** @var Revision $rev */
499 $rev = $status->value
['revision'];
501 $store = MediaWikiServices
::getInstance()->getRevisionStore();
502 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
504 $this->assertSame( $rev->getId(), $revRecord->getId() );
505 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
506 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
510 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
512 public function testGetRevisionByPageId() {
513 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
514 $content = new WikitextContent( __METHOD__
);
515 $status = $page->doEditContent( $content, __METHOD__
);
516 /** @var Revision $rev */
517 $rev = $status->value
['revision'];
519 $store = MediaWikiServices
::getInstance()->getRevisionStore();
520 $revRecord = $store->getRevisionByPageId( $page->getId() );
522 $this->assertSame( $rev->getId(), $revRecord->getId() );
523 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
524 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
528 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
530 public function testGetRevisionByTimestamp() {
531 // Make sure there is 1 second between the last revision and the rev we create...
532 // Otherwise we might not get the correct revision and the test may fail...
535 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
536 $content = new WikitextContent( __METHOD__
);
537 $status = $page->doEditContent( $content, __METHOD__
);
538 /** @var Revision $rev */
539 $rev = $status->value
['revision'];
541 $store = MediaWikiServices
::getInstance()->getRevisionStore();
542 $revRecord = $store->getRevisionByTimestamp(
547 $this->assertSame( $rev->getId(), $revRecord->getId() );
548 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
549 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
552 private function revisionToRow( Revision
$rev ) {
553 $page = WikiPage
::factory( $rev->getTitle() );
556 'rev_id' => (string)$rev->getId(),
557 'rev_page' => (string)$rev->getPage(),
558 'rev_text_id' => (string)$rev->getTextId(),
559 'rev_timestamp' => (string)$rev->getTimestamp(),
560 'rev_user_text' => (string)$rev->getUserText(),
561 'rev_user' => (string)$rev->getUser(),
562 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
563 'rev_deleted' => (string)$rev->getVisibility(),
564 'rev_len' => (string)$rev->getSize(),
565 'rev_parent_id' => (string)$rev->getParentId(),
566 'rev_sha1' => (string)$rev->getSha1(),
567 'rev_comment_text' => $rev->getComment(),
568 'rev_comment_data' => null,
569 'rev_comment_cid' => null,
570 'rev_content_format' => $rev->getContentFormat(),
571 'rev_content_model' => $rev->getContentModel(),
572 'page_namespace' => (string)$page->getTitle()->getNamespace(),
573 'page_title' => $page->getTitle()->getDBkey(),
574 'page_id' => (string)$page->getId(),
575 'page_latest' => (string)$page->getLatest(),
576 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
577 'page_len' => (string)$page->getContent()->getSize(),
578 'user_name' => (string)$rev->getUserText(),
582 private function assertRevisionRecordMatchesRevision(
584 RevisionRecord
$record
586 $this->assertSame( $rev->getId(), $record->getId() );
587 $this->assertSame( $rev->getPage(), $record->getPageId() );
588 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
589 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
590 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
591 $this->assertSame( $rev->isMinor(), $record->isMinor() );
592 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
593 $this->assertSame( $rev->getSize(), $record->getSize() );
595 * @note As of MW 1.31, the database schema allows the parent ID to be
596 * NULL to indicate that it is unknown.
598 $expectedParent = $rev->getParentId();
599 if ( $expectedParent === null ) {
602 $this->assertSame( $expectedParent, $record->getParentId() );
603 $this->assertSame( $rev->getSha1(), $record->getSha1() );
604 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
605 $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
606 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
607 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
611 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
612 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
614 public function testNewRevisionFromRow_anonEdit() {
615 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
616 $text = __METHOD__
. 'a-ä';
617 /** @var Revision $rev */
618 $rev = $page->doEditContent(
619 new WikitextContent( $text ),
621 )->value
['revision'];
623 $store = MediaWikiServices
::getInstance()->getRevisionStore();
624 $record = $store->newRevisionFromRow(
625 $this->revisionToRow( $rev ),
629 $this->assertRevisionRecordMatchesRevision( $rev, $record );
630 $this->assertSame( $text, $rev->getContent()->serialize() );
634 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
635 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
637 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
638 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
639 $this->overrideMwServices();
640 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
641 $text = __METHOD__
. 'a-ä';
642 /** @var Revision $rev */
643 $rev = $page->doEditContent(
644 new WikitextContent( $text ),
646 )->value
['revision'];
648 $store = MediaWikiServices
::getInstance()->getRevisionStore();
649 $record = $store->newRevisionFromRow(
650 $this->revisionToRow( $rev ),
654 $this->assertRevisionRecordMatchesRevision( $rev, $record );
655 $this->assertSame( $text, $rev->getContent()->serialize() );
659 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
660 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
662 public function testNewRevisionFromRow_userEdit() {
663 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
664 $text = __METHOD__
. 'b-ä';
665 /** @var Revision $rev */
666 $rev = $page->doEditContent(
667 new WikitextContent( $text ),
671 $this->getTestUser()->getUser()
672 )->value
['revision'];
674 $store = MediaWikiServices
::getInstance()->getRevisionStore();
675 $record = $store->newRevisionFromRow(
676 $this->revisionToRow( $rev ),
680 $this->assertRevisionRecordMatchesRevision( $rev, $record );
681 $this->assertSame( $text, $rev->getContent()->serialize() );
685 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
687 public function testNewRevisionFromArchiveRow() {
688 $store = MediaWikiServices
::getInstance()->getRevisionStore();
689 $title = Title
::newFromText( __METHOD__
);
690 $text = __METHOD__
. '-bä';
691 $page = WikiPage
::factory( $title );
692 /** @var Revision $orig */
693 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
695 $page->doDeleteArticle( __METHOD__
);
697 $db = wfGetDB( DB_MASTER
);
698 $arQuery = $store->getArchiveQueryInfo();
700 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
701 __METHOD__
, [], $arQuery['joins']
703 $this->assertTrue( is_object( $res ), 'query failed' );
705 $row = $res->fetchObject();
707 $record = $store->newRevisionFromArchiveRow( $row );
709 $this->assertRevisionRecordMatchesRevision( $orig, $record );
710 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
714 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
716 public function testNewRevisionFromArchiveRow_legacyEncoding() {
717 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
718 $this->overrideMwServices();
719 $store = MediaWikiServices
::getInstance()->getRevisionStore();
720 $title = Title
::newFromText( __METHOD__
);
721 $text = __METHOD__
. '-bä';
722 $page = WikiPage
::factory( $title );
723 /** @var Revision $orig */
724 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
726 $page->doDeleteArticle( __METHOD__
);
728 $db = wfGetDB( DB_MASTER
);
729 $arQuery = $store->getArchiveQueryInfo();
731 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
732 __METHOD__
, [], $arQuery['joins']
734 $this->assertTrue( is_object( $res ), 'query failed' );
736 $row = $res->fetchObject();
738 $record = $store->newRevisionFromArchiveRow( $row );
740 $this->assertRevisionRecordMatchesRevision( $orig, $record );
741 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
745 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
747 public function testLoadRevisionFromId() {
748 $title = Title
::newFromText( __METHOD__
);
749 $page = WikiPage
::factory( $title );
750 /** @var Revision $rev */
751 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
754 $store = MediaWikiServices
::getInstance()->getRevisionStore();
755 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
756 $this->assertRevisionRecordMatchesRevision( $rev, $result );
760 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
762 public function testLoadRevisionFromPageId() {
763 $title = Title
::newFromText( __METHOD__
);
764 $page = WikiPage
::factory( $title );
765 /** @var Revision $rev */
766 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
769 $store = MediaWikiServices
::getInstance()->getRevisionStore();
770 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
771 $this->assertRevisionRecordMatchesRevision( $rev, $result );
775 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
777 public function testLoadRevisionFromTitle() {
778 $title = Title
::newFromText( __METHOD__
);
779 $page = WikiPage
::factory( $title );
780 /** @var Revision $rev */
781 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
784 $store = MediaWikiServices
::getInstance()->getRevisionStore();
785 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
786 $this->assertRevisionRecordMatchesRevision( $rev, $result );
790 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
792 public function testLoadRevisionFromTimestamp() {
793 $title = Title
::newFromText( __METHOD__
);
794 $page = WikiPage
::factory( $title );
795 /** @var Revision $revOne */
796 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
798 // Sleep to ensure different timestamps... )(evil)
800 /** @var Revision $revTwo */
801 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
804 $store = MediaWikiServices
::getInstance()->getRevisionStore();
806 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
810 $store->loadRevisionFromTimestamp(
811 wfGetDB( DB_MASTER
),
813 $revOne->getTimestamp()
818 $store->loadRevisionFromTimestamp(
819 wfGetDB( DB_MASTER
),
821 $revTwo->getTimestamp()
827 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
829 public function testGetParentLengths() {
830 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
831 /** @var Revision $revOne */
832 $revOne = $page->doEditContent(
833 new WikitextContent( __METHOD__
), __METHOD__
834 )->value
['revision'];
835 /** @var Revision $revTwo */
836 $revTwo = $page->doEditContent(
837 new WikitextContent( __METHOD__
. '2' ), __METHOD__
838 )->value
['revision'];
840 $store = MediaWikiServices
::getInstance()->getRevisionStore();
843 $revOne->getId() => strlen( __METHOD__
),
845 $store->listRevisionSizes(
846 wfGetDB( DB_MASTER
),
852 $revOne->getId() => strlen( __METHOD__
),
853 $revTwo->getId() => strlen( __METHOD__
) +
1,
855 $store->listRevisionSizes(
856 wfGetDB( DB_MASTER
),
857 [ $revOne->getId(), $revTwo->getId() ]
863 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
865 public function testGetPreviousRevision() {
866 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
867 /** @var Revision $revOne */
868 $revOne = $page->doEditContent(
869 new WikitextContent( __METHOD__
), __METHOD__
870 )->value
['revision'];
871 /** @var Revision $revTwo */
872 $revTwo = $page->doEditContent(
873 new WikitextContent( __METHOD__
. '2' ), __METHOD__
874 )->value
['revision'];
876 $store = MediaWikiServices
::getInstance()->getRevisionStore();
878 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
882 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
887 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
889 public function testGetNextRevision() {
890 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
891 /** @var Revision $revOne */
892 $revOne = $page->doEditContent(
893 new WikitextContent( __METHOD__
), __METHOD__
894 )->value
['revision'];
895 /** @var Revision $revTwo */
896 $revTwo = $page->doEditContent(
897 new WikitextContent( __METHOD__
. '2' ), __METHOD__
898 )->value
['revision'];
900 $store = MediaWikiServices
::getInstance()->getRevisionStore();
903 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
906 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
911 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
913 public function testGetTimestampFromId_found() {
914 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
915 /** @var Revision $rev */
916 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
919 $store = MediaWikiServices
::getInstance()->getRevisionStore();
920 $result = $store->getTimestampFromId(
925 $this->assertSame( $rev->getTimestamp(), $result );
929 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
931 public function testGetTimestampFromId_notFound() {
932 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
933 /** @var Revision $rev */
934 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
937 $store = MediaWikiServices
::getInstance()->getRevisionStore();
938 $result = $store->getTimestampFromId(
943 $this->assertFalse( $result );
947 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
949 public function testCountRevisionsByPageId() {
950 $store = MediaWikiServices
::getInstance()->getRevisionStore();
951 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
955 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
957 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
960 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
962 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
965 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
970 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
972 public function testCountRevisionsByTitle() {
973 $store = MediaWikiServices
::getInstance()->getRevisionStore();
974 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
978 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
980 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
983 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
985 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
988 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
993 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
995 public function testUserWasLastToEdit_false() {
996 $sysop = $this->getTestSysop()->getUser();
997 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
998 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1000 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1001 $result = $store->userWasLastToEdit(
1002 wfGetDB( DB_MASTER
),
1007 $this->assertFalse( $result );
1011 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1013 public function testUserWasLastToEdit_true() {
1014 $startTime = wfTimestampNow();
1015 $sysop = $this->getTestSysop()->getUser();
1016 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1017 $page->doEditContent(
1018 new WikitextContent( __METHOD__
),
1025 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1026 $result = $store->userWasLastToEdit(
1027 wfGetDB( DB_MASTER
),
1032 $this->assertTrue( $result );
1036 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1038 public function testGetKnownCurrentRevision() {
1039 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1040 /** @var Revision $rev */
1041 $rev = $page->doEditContent(
1042 new WikitextContent( __METHOD__
. 'b' ),
1046 $this->getTestUser()->getUser()
1047 )->value
['revision'];
1049 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1050 $record = $store->getKnownCurrentRevision(
1055 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1058 public function provideNewMutableRevisionFromArray() {
1059 yield
'Basic array, with page & id' => [
1064 'timestamp' => '20171017114835',
1065 'user_text' => '111.0.1.2',
1067 'minor_edit' => false,
1071 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1072 'comment' => 'Goat Comment!',
1073 'content_format' => 'text/x-wiki',
1074 'content_model' => 'wikitext',
1077 yield
'Basic array, content object' => [
1081 'timestamp' => '20171017114835',
1082 'user_text' => '111.0.1.2',
1084 'minor_edit' => false,
1088 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1089 'comment' => 'Goat Comment!',
1090 'content' => new WikitextContent( 'Some Content' ),
1093 yield
'Basic array, serialized text' => [
1097 'timestamp' => '20171017114835',
1098 'user_text' => '111.0.1.2',
1100 'minor_edit' => false,
1104 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1105 'comment' => 'Goat Comment!',
1106 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1109 yield
'Basic array, serialized text, utf-8 flags' => [
1113 'timestamp' => '20171017114835',
1114 'user_text' => '111.0.1.2',
1116 'minor_edit' => false,
1120 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1121 'comment' => 'Goat Comment!',
1122 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1126 yield
'Basic array, with title' => [
1128 'title' => Title
::newFromText( 'SomeText' ),
1130 'timestamp' => '20171017114835',
1131 'user_text' => '111.0.1.2',
1133 'minor_edit' => false,
1137 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1138 'comment' => 'Goat Comment!',
1139 'content_format' => 'text/x-wiki',
1140 'content_model' => 'wikitext',
1143 yield
'Basic array, no user field' => [
1148 'timestamp' => '20171017114835',
1149 'user_text' => '111.0.1.3',
1150 'minor_edit' => false,
1154 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1155 'comment' => 'Goat Comment!',
1156 'content_format' => 'text/x-wiki',
1157 'content_model' => 'wikitext',
1163 * @dataProvider provideNewMutableRevisionFromArray
1164 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1166 public function testNewMutableRevisionFromArray( array $array ) {
1167 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1169 $result = $store->newMutableRevisionFromArray( $array );
1171 if ( isset( $array['id'] ) ) {
1172 $this->assertSame( $array['id'], $result->getId() );
1174 if ( isset( $array['page'] ) ) {
1175 $this->assertSame( $array['page'], $result->getPageId() );
1177 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1178 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1179 if ( isset( $array['user'] ) ) {
1180 $this->assertSame( $array['user'], $result->getUser()->getId() );
1182 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1183 $this->assertSame( $array['deleted'], $result->getVisibility() );
1184 $this->assertSame( $array['len'], $result->getSize() );
1185 $this->assertSame( $array['parent_id'], $result->getParentId() );
1186 $this->assertSame( $array['sha1'], $result->getSha1() );
1187 $this->assertSame( $array['comment'], $result->getComment()->text
);
1188 if ( isset( $array['content'] ) ) {
1190 $result->getSlot( 'main' )->getContent()->equals( $array['content'] )
1192 } elseif ( isset( $array['text'] ) ) {
1193 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1196 $array['content_format'],
1197 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1199 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1204 * @dataProvider provideNewMutableRevisionFromArray
1205 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1207 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1208 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1209 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
1210 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1212 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1213 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1214 ->disableOriginalConstructor()
1216 $factory->expects( $this->any() )
1217 ->method( 'newBlobStore' )
1218 ->willReturn( $blobStore );
1219 $factory->expects( $this->any() )
1220 ->method( 'newSqlBlobStore' )
1221 ->willReturn( $blobStore );
1223 $this->setService( 'BlobStoreFactory', $factory );
1225 $this->testNewMutableRevisionFromArray( $array );